#ifndef frm_control_TRow_H
#define frm_control_TRow_H

#include "frm_control_TLine.h"

namespace frm { namespace control {

///////////////////////////////////////////////////////////////////////////////
struct TRow;
///////////////////////////////////////////////////////////////////////////////

struct TRowCaption : public TLine
{
  struct TStyle : public TLine::TStyle
  {
    svl_Type(TLine::TStyle)
  };
  //
  TRow &Row; 
  //
  svl_Type(TLine)
  TRowCaption(TRow &Row_);
  //
  virtual void TComponent_SelectEnter();
  virtual void TComponent_SelectExit();
  virtual void TComponent_KeyboardKey(grf::TKeyboardKey &KeyboardKey);
  virtual void TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles);
};

///////////////////////////////////////////////////////////////////////////////

struct TRowChildrenIndent : public TPanel
{
  struct TStyle : public TPanel::TStyle
  {
    svl_Type(TPanel::TStyle)
    int Width;
    //
    TStyle() { SetDefault();}
    void SetDefault();
    //
    virtual void SetFromParent(frm::TStyle *Parent);
    virtual svl::TInfoParam AddInfoTo(svl::TInfoParam Info);
  };
  //
  TRow &Row;
  //
  TRowChildrenIndent(TRow &Row);
  //
  TRowChildrenIndent &SetStyle(TStyle &V);
  virtual void TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles);

private:
  TStyle *style;
};

///////////////////////////////////////////////////////////////////////////////

struct TRowChildren : public TPanel
{
  struct TStyle : public TPanel::TStyle
  {
    svl_Type(TPanel::TStyle)
  };
  //
  TRow &Row;
  //
  TRowChildren(TRow &Row);
  //
  virtual void DeleteChild(int N);
  virtual void DeleteChild(TComponent *Del) { TPanel::DeleteChild(Del);}
  virtual void TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles);
};

///////////////////////////////////////////////////////////////////////////////

struct TRow : public TPanel
{
  struct TStyle : public frm::TStyle
  {
    svl_Type(frm::TStyle)
    
    TPanel::TStyle             Normal;
    TRowCaption::TStyle        Caption;
    TRowChildrenIndent::TStyle Indent;
    TRowChildren::TStyle       Children;
    //
    virtual void SetFromParent(frm::TStyle *Parent);
    virtual svl::TInfoParam AddInfoTo(svl::TInfoParam Info);
  };
  
  
  
/******************  
  ////////////////////////////////////////////////////////
  struct TEventsList; struct TEvents : public svl::TEvents
  {
    ///////////////////////////////////////////
    struct TParam : public svl::TEvents::TParam
    {
      TRow &Sender;
      //
      TParam(TEventsList &eventsList) : svl::TEvents::TParam(eventsList),Sender(eventsList.Sender) {}
    };
    ////////////////////////////////////
    struct TBeforeExpand : public TParam
    {
      bool &EnExpand;
      //
      TBeforeExpand(TEventsList &eventsList,bool &enExpand) : TParam(eventsList),EnExpand(enExpand) {}
    };
    virtual void TEvents_BeforeExpand(TBeforeExpand &Param) { TEvents *P = (TEvents*)Param.GetPrev(); if (P) { P->TEvents_BeforeExpand(Param);} }

    ///////////////////////////////////
    struct TAfterExpand : public TParam
    {
      TAfterExpand(TEventsList &eventsList) : TParam(eventsList) {}
    };
    virtual void TEvents_AfterExpand(TAfterExpand &Param) { TEvents *P = (TEvents*)Param.GetPrev(); if (P) { P->TEvents_AfterExpand(Param);} }

    //////////////////////////////////////
    struct TBeforeCollapse : public TParam
    {
      bool &EnCollapse;
      //
      TBeforeCollapse(TEventsList &eventsList,bool &enCollapse) : TParam(eventsList),EnCollapse(enCollapse) {}
    };
    virtual void TEvents_BeforeCollapse(TBeforeCollapse &Param) { TEvents *P = (TEvents*)Param.GetPrev(); if (P) { P->TEvents_BeforeCollapse(Param);} }

    /////////////////////////////////////
    struct TAfterCollapse : public TParam
    {
      TAfterCollapse(TEventsList &eventsList) : TParam(eventsList) {}
    };
    virtual void TEvents_AfterCollapse(TAfterCollapse &Param) { TEvents *P = (TEvents*)Param.GetPrev(); if (P) { P->TEvents_AfterCollapse(Param);} }
  };
  ////////////////////////////////////////////
  struct TEventsList : public svl::TEventsList
  {
    TRow &Sender;
    //
    TEventsList(TRow &sender) : Sender(sender),svl::TEventsList(Default()) {}
    //
    void Link  (TEvents &V) { svl::TEventsList::Link  (V);}
    void Unlink(TEvents &V) { svl::TEventsList::Unlink(V);}
    //
    TEvents &Get() { return (TEvents&)svl::TEventsList::GetBot();}
    
  private:
    static TEvents &Default() { static TEvents E; return E;}
  }
  Events;
****************************/


public:
  TRowCaption        *Caption;
  TPanel             *ChildrenPanel;
  TRowChildrenIndent   *ChildrenIndent;
  TRowChildren         *Children;
  //
  TRow(TWindow &Window);
  svl_Type(TPanel)
  //
  void BeforeDeleteRow();
  //
  void SetStyle(TStyle &V);
  //
  bool HasChildren()     { return Children->GetChildrenCount() > 0; }
  bool GetExpand()       { return ChildrenPanel->GetVisible();}
  void SetExpand(bool V) { ChildrenPanel->SetVisible(V);}
  virtual void UpdateExpand(bool V);
  void ExpandAll();
  //
  int CalcNumNodeLevel();

protected:
  virtual void TRowCaption_SelectEnter() {}
  virtual void TRowCaption_SelectExit() {}
  virtual void TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles);
  
private:
  void beforeDeleteRow(TPanel *Child,TTable *Table);
  friend TRowCaption;
};

////////////////////////////////////////////////////////////////////////////////

struct TRowExpand : public TPanel
{
  struct TStyle : public frm::TStyle
  {
    svl_Type(frm::TStyle)
    TPanel::TStyle NoneChildren,Collapse,Expand;
    //
    TStyle() { SetDefault();}
    void SetDefault();
    //
    virtual void SetFromParent(frm::TStyle *Parent);
    virtual svl::TInfoParam AddInfoTo(svl::TInfoParam Info);
  };
  //
  TRowExpand(TWindow &Window);
  TRowExpand(TRow &Row);
  //
  void SetRow(TRow *Row) { row = Row;}
  TRowExpand &SetStyle(TStyle &V);
  //
  virtual void TComponent_Paint(TPaint &Paint,int NPaintStack);
  virtual void TComponent_Mouse(TMouse  Mouse);
  virtual bool TComponent_TransparentForFocus() { return false;}
  virtual void TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles);

private:
  TRow *row;
  TStyle *style;
  svl_Except;
};

///////////////////////////////////////////////////////////////////////////////
}}

#endif
